package com.leyou.auth.config;

import com.leyou.auth.interceptors.LoginInterceptor;
import com.leyou.auth.utils.JwtUtils;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
    private JwtUtils jwtUtils;
    private ClientProperties clientProperties;

    public MvcConfiguration(JwtUtils jwtUtils, ClientProperties clientProperties) {
        this.jwtUtils = jwtUtils;
        this.clientProperties = clientProperties;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截注册器并注册
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new LoginInterceptor(jwtUtils));
        //判断用户是否配置了拦截路径,没走默认,有就拦截
        if (!CollectionUtils.isEmpty(clientProperties.getIncludeFilterPaths())){
            interceptorRegistration.addPathPatterns(clientProperties.getIncludeFilterPaths());
        }
        //判断是否配置放行路径,美就是空
        if (!CollectionUtils.isEmpty(clientProperties.getExcludeFilterPaths())){
            interceptorRegistration.excludePathPatterns(clientProperties.getExcludeFilterPaths());
        }
    }
}
